<template>
  <div style="width: 100%">
    <el-form
      :disabled="disabled"
      :model="formData"
      size="small"
      :rules="rules"
      ref="form"
      label-width="100px"
    >
      <el-row>
        <el-col :span="23">
          <el-form-item label="中文名称" prop="nameCn">
            <el-input v-model="formData.nameCn"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="23">
          <el-form-item label="英文名称" prop="nameEn">
            <el-input v-model="formData.nameEn"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="23">
          <el-form-item v-if="tabType === 'page'" label="页面名称" prop="code">
            <el-input v-model="formData.code"></el-input>
          </el-form-item>
          <el-form-item v-else label="布局名称" prop="name">
            <el-input v-model="formData.name"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="23">
          <el-form-item label="中文描述">
            <el-input v-model="formData.describeCn"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="23">
          <el-form-item label="英文描述">
            <el-input v-model="formData.describeEn"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="23">
          <el-form-item label="标签">
            <el-input v-model="formData.label" readonly>
              <el-button slot="append">添加</el-button>
            </el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <div class="md-component-btns">
      <el-button
        size="medium"
        type="primary"
        v-if="disabled"
        @click="dialogType = 'edit'"
        >编辑</el-button
      >
      <el-button size="medium" type="primary" v-if="!disabled" @click="save"
        >保存</el-button
      >
      <el-button size="medium" @click="$emit('removeThisTabs')">取消</el-button>
    </div>
  </div>
</template>
<script>
import * as api from "@/api/applicationManage/applicationManage";

export default {
  name: "applicationManageTemp",
  data() {
    return {
      formData: {
        nameCn: "",
        nameEn: "",
        describeCn: "",
        describeEn: "",
        applicationId: "",
        label: "",
        code: "",
      },
      rules: {
        nameCn: [
          { required: true, message: "请输入中文名称", trigger: "blur" },
        ],
        nameEn: [
          { required: true, message: "请输入英文名称", trigger: "blur" },
        ],
        code: [{ required: true, message: "请输入页面名称", trigger: "blur" }],
        name: [{ required: true, message: "请输入页面名称", trigger: "blur" }],
      },

      dialogType: "open",
      tabType: "page",
    };
  },
  computed: {
    disabled() {
      return this.dialogType === "view";
    },
  },
  methods: {
    save() {
      this.$refs.form.validate((f) => {
        if (f) {
          let applicationId = localStorage.getItem("appId");
          let method = this.dialogType === "open" ? "add" : "update";
          if (this.tabType === "layout") {
            method = this.dialogType === "open" ? "addLayout" : "updateLayout";
            this.formData = {
              ...this.formData,
              name: this.formData.name || this.formData.code,
              applicationId: this.formData.applicationId || applicationId,
            };
          }
          api[method](this.formData).then(() => {
            this.$message.success("保存成功");
            this.$emit("refresh");
            this.$emit("removeThisTabs");
          });
        }
      });
    },

    open(row) {
      this.dialogType = "open";
      this.tabType = row.tabType;
    },
    edit(row) {
      this.tabType = row.tabType;
      this.getDetails(row.oid);
      this.dialogType = "edit";
    },
    view(row) {
      this.tabType = row.tabType;
      this.getDetails(row.oid);
      this.dialogType = "view";
    },
    getDetails(oid) {
      let method = this.tabType === "page" ? "detail" : "detailLayout";
      const paramsData = this.tabType === "page" ? { id: oid } : { oid };
      api[method](paramsData).then((res) => {
        if (res.data.data) {
          this.formData = res.data.data;
        }
      });
    },
  },
  mounted() {
    let that = this;
    document.addEventListener("ApplicationId", function (event) {
      if (event.detail) {
        that.formData.applicationId = event.detail;
      }
    });
  },
};
</script>
<style scoped>
</style>
